package com.sniper.utils;

import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.TimeZone;

/**
 * @author suzhen
 */
public class DateUtil {


    /**
     * 获取之前几天的时间
     *
     * @param amountToSubtract
     * @param unit
     * @return
     */
    public static String getStringBefore(Instant instant, long amountToSubtract, ChronoUnit unit, ZoneId zoneId) {

        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zoneId);
        LocalDateTime _localDateTime = localDateTime.minus(amountToSubtract, unit);
        ZonedDateTime zonedDateTime = ZonedDateTime.of(_localDateTime, zoneId);
        return zonedDateTime.format(DateTimeFormatter.ISO_INSTANT);
    }

    public static Date getDateBefore(Instant instant, long amountToSubtract, ChronoUnit unit, ZoneId zoneId) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zoneId);
        LocalDateTime _localDateTime = localDateTime.minus(amountToSubtract, unit);
        Instant _instant = _localDateTime.toInstant(ZoneOffset.ofHours(8));
        return Date.from(_instant);
    }

    /**
     * 获取之后几天的时间
     *
     * @param amountToSubtract
     * @param unit
     * @return
     */
    public static String getStringAfter(Instant instant, long amountToSubtract, ChronoUnit unit, ZoneId zoneId) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zoneId);
        LocalDateTime _localDateTime = localDateTime.plus(amountToSubtract, unit);
        ZonedDateTime zonedDateTime = ZonedDateTime.of(_localDateTime, zoneId);
        return zonedDateTime.format(DateTimeFormatter.ISO_INSTANT);
    }

    public static Date getDateAfter(Instant instant, long amountToSubtract, ChronoUnit unit, ZoneId zoneId) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zoneId);
        LocalDateTime _localDateTime = localDateTime.plus(amountToSubtract, unit);
        Instant _instant = _localDateTime.toInstant(ZoneOffset.ofHours(8));
        return Date.from(_instant);
    }

    public static String UTCFormat(Date date) {
        TimeZone timeZone = TimeZone.getTimeZone("UTC");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd^hh:mm:ss");
        format.setTimeZone(timeZone);
        return format.format(date).replace("^", "T") + "Z";
    }
}
